/**
 * OSS is a Open Service Container which gives the leverage of building the Desktop and Web
 * Application. * Copyright (C) 2008-2009  Ponraj Sutanthiramani
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Or5e Open Source Community
 * Navigator Bldg, ITPL, Bangalore, India.
 */

package org.or5e.api.webcam;

import javax.media.Buffer;
import javax.media.Codec;
import javax.media.Format;
import javax.media.format.VideoFormat;
import org.or5e.api.webcam.util.ElapsedTimer;

/**
 * @author Ponraj Suthanthiramani
 */
public class PreAccessCodec implements Codec {
        ElapsedTimer timer = new ElapsedTimer();
        Format input = null;
        Format output = null;

        void accessFrame(Buffer frame) {
            timer.reset();
        }
        protected Format supportedIns[] = new Format[]{new VideoFormat(null)};
        protected Format supportedOuts[] = new Format[]{new VideoFormat(null)};

        @Override
        public String getName() {return "Pre-Access Codec";}

        @Override public void open() {}

        @Override public void close() {}

        @Override public void reset() {}

        @Override public Format[] getSupportedInputFormats() {
            return supportedIns;
        }

        @Override public Format[] getSupportedOutputFormats(Format in) {
            if (in == null) {
                return supportedOuts;
            } else {
                Format outs[] = new Format[1];
                outs[0] = in;
                return outs;
            }
        }

        @Override public Format setInputFormat(Format format) {
            input = format;
            return input;
        }

        @Override public Format setOutputFormat(Format format) {
            output = format;
            return output;
        }
        @Override public int process(Buffer in, Buffer out) {
            accessFrame(in);

            Object data = in.getData();
            in.setData(out.getData());
            out.setData(data);

            out.setFormat(in.getFormat());
            out.setLength(in.getLength());
            out.setOffset(in.getOffset());

            return BUFFER_PROCESSED_OK;
        }

        @Override public Object[] getControls() {
            return new Object[0];
        }

        @Override public Object getControl(String type) {
            return null;
        }
    }